import { createRouter, createWebHistory } from 'vue-router'
//import Cookies from 'js-cookie'
import AppLayout from '../components/layout/AppLayout.vue'
import IndexView from '@/views/IndexView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import("@/views/login/LoginView.vue")
    },
    {
      path: '/home',
      name: 'home',
      component: AppLayout,
      children:[
        {
          path: '',
          component: IndexView,
        },
        {
          path: '/book/add',
          component: () =>  import("@/views/book/AddBook.vue")
        },
        {
          path: '/book/search',
          component: () =>  import("@/views/book/SearchBook.vue")
        },
        {
          path: '/category/add',
          component: () =>  import("@/views/category/AddCategory.vue")
        },
        {
          path: '/userlist',
          component: () =>  import("@/views/user/SearchUser.vue")
        },
        {
          path: '/user/add',
          component: () =>  import("@/views/user/AddUser.vue")
        },
        {
          path: '/borrowing',
          component: () =>  import("@/views/borrowing/BorrowingView.vue")
        },
        {
          path: '/admin/profile',
          component: () => import("@/views/ProfileView.vue")
        }
      ]
    },
    {
      path: '/user/profile',
      name: 'profile',
      component: () => import("@/views/user/UserProfile.vue")
    }
  ],
})

// router.beforeEach((to, from, next) => {
//   if (to.path === '/') next()
//   const admin = Cookies.get("admin")
//   if (!admin && to.path !== '/') return next("/")  // 强制退回到登录页面
//   // 访问 /home 的时候，并且cookie里面存在数据，这个时候我就直接放行
//   next()
// })

export default router
